<?php 

class Event extends BaseTopic implements iEditEverything {

	private $region_manager;

	function __construct($page, $id = 0) {
		parent::__construct($page, $id);
		$this->region_manager = new RegionManager();
	}

	function set_region_manager($region_manager) {
		$this->region_manager = $region_manager;
	}

	public function prepare($new, $item, $inputs) {

		if ($_POST['region'] === "NEW") {
			$kv = $this->region_manager->region_get_regions_kv(1);
			$inputs[] = $this->creator->input_field("Regio", "region_name");
			$inputs[] = $this->creator->input_select("Locatie", "region", $kv['keys'], $kv['values'], "kies een regio", $item['region_id']);
		} else {
			$kv = $this->region_manager->region_get_regions_kv();
			$inputs[] = $this->creator->input_select("Regio", "region", array_merge(array("NEW"), $kv['keys']), array_merge(array("NIEUWE REGIO"), $kv['values']), "kies een regio", $item['region_id'], "document.itemform.submit()");
		}
		
		if (!$new) {
			$inputs[] = $this->creator->input_checkbox("Een nieuwe editie (datum) invoeren en de vorige archiveren", "archive");
		}

		$inputs[] = $this->creator->input_date(
				"Begindatum", 
				"date_start", 
				array(
						'day' => $item['date_start_day'], 
						'month' => $item['date_start_month'], 
						'year' => $item['date_start_year']));
		
		$inputs[] = $this->creator->input_checkbox("Meerdaags evenement", "multiple_days", (!$_POST['item_id'] && $item['multiple_days']) || $_POST['multiple_days'], "document.itemform.submit()"); //TODO: extract javascript
		if ((!$_POST['item_id'] && $item['multiple_days']) || $_POST['multiple_days']) {
			$inputs[] = $this->creator->input_date("Einddatum", "date_end", array('day' => $item['date_end_day'], 'month' => $item['date_end_month'], 'year' => $item['date_end_year']));
		}

		$inputs[] = $this->creator->input_field("Link", "link", $this->db_access->db_desqlize($item['link']));
		$inputs[] = $this->creator->input_text("Beschrijving", "description", $this->db_access->db_desqlize($item['description']));
		return $inputs;
	}

	public function check_input($values, $new) {
		$ok = $this->checker->check_tags_advanced($values['description'], "beschrijving");
		$ok = $this->checker->check_tag_closures($values['description']) && $ok;
		$ok = $this->checker->check_exists($values['description'], "beschrijving") && $ok;
		if (ereg("^parent[0-9]+$", $values['region'])) {
			$ok = $this->checker->check_exists($values['region_name'], "naam regio") && $ok;
			$ok = $this->checker->check_tags($values['region_name'], "naam regio") && $ok;
		} else {
			$result = $this->checker->check_input($values['region'] > 0, "regio niet aangegeven");
		}
		if ($values['link']) {
			$ok = $this->checker->check_url($values['link'], "link") && $ok;
		}
		$ok = $ok && $this->checker->check_date_valid($values['date_start_day'], $values['date_start_month'], $values['date_start_year'], "datum");
		$ok = $ok && $this->checker->check_date_future($values['date_start_day'], $values['date_start_month'], $values['date_start_year'], "datum");
		if ($values['date_end_day']) {
			$ok = $ok && $this->checker->check_date_valid($values['date_end_day'], $values['date_end_month'], $values['date_end_year'], "datum");
			$ok = $ok && $this->checker->check_date_future($values['date_end_day'], $values['date_end_month'], $values['date_end_year'], "datum");
		}
		return $ok;
	}

	public function get_edit_everything() {
		$sql = $this->db_access->db_query_select(array(array('*')), array("EVENT"), "item_id = {$this->id}");
		$sqlresult = $this->db_access->db_do_query($sql);
		return $this->db_access->db_fetch_array($sqlresult);
	}

	protected function derive_info() {
		include_once('sys/regionfunctions.inc');
		$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";
		$info = date_format($date, 0, -1, 1) . " - ";
		$info .= region_format(region_get_region_tree($_POST['region'])); 
		return $info;
	}

	protected function get_sql_from_post($item_id) {
		if (ereg("^parent[0-9]+$", $_POST['region'])) { // new region
			db_do_query($page, db_query_insert(array('name'), array(db_sqlize($_POST['region_name'])), "REGION"));
			$region_id = db_insert_id();
			db_do_query($page, db_query_insert(array('parent_region', 'sub_region'), array(substr($_POST['region'], 6), $region_id), "REGION_TREE"));
		} else
			$region_id = $_POST['region'];
			
		$date = $_POST['date_start_year'] . (($_POST['date_start_month'] > 9) ? "{$_POST['date_start_month']}" : "0{$_POST['date_start_month']}") . (($_POST['date_start_day'] > 9) ? "{$_POST['date_start_day']}" : "0{$_POST['date_start_day']}") . "000000";	
		if ($_POST['date_end_day'])
			$enddate = $_POST['date_end_year'] . (($_POST['date_end_month'] > 9) ? "{$_POST['date_end_month']}" : "0{$_POST['date_end_month']}") . (($_POST['date_end_day'] > 9) ? "{$_POST['date_end_day']}" : "0{$_POST['date_end_day']}") . "000000";
		else
			$enddate = "NULL";
		return db_query_insert(array('item_id', 'region_id', 'description', 'link', 'date_start', 'multiple_days', 'date_end'), array($item_id, $region_id, db_sqlize($_POST['description']), db_sqlize($_POST['link']), $date, $_POST['multiple_days'] ? 1 : 0, $enddate), "EVENT");
	}
}
?>
